package com.liu.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.commonutils.ReturnResult;
import com.liu.commonutils.constant.Constants;
import com.liu.eduservice.entity.EduCourse;
import com.liu.eduservice.entity.vo.CourseInfoForm;
import com.liu.eduservice.entity.vo.CoursePublishVo;
import com.liu.eduservice.entity.vo.CourseQuery;
import com.liu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
@Api("课程表管理")
public class EduCourseController {


    @Autowired
    EduCourseService eduCourseService;


    // 添加课程信息
    @PostMapping("/saveCourseInfo")
    @ApiOperation("向课程表添加课程信息")
    public ReturnResult saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        // 返回添加之后课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoForm);
        return ReturnResult.ok().data("courseId", id);
    }

    // 根据课程id查询课程(需要查询EduCourse和EduCourseDescription，封装为CourseInfoForm对象)
    @GetMapping("/getCourse/{courseId}")
    @ApiOperation("根据课程id查询课程")
    public ReturnResult getCourseInfoById(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return ReturnResult.ok().data("courseInfo", courseInfoForm);
    }

    // 根据前端传递来的表单对象CourseInfoForm，修改课程信息EduCourse和EduCourseDescription
    @PostMapping("/updateCourseInfo")
    @ApiOperation("修改课程信息")
    public ReturnResult updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return ReturnResult.ok();
    }

    // 根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public ReturnResult getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(courseId);
        return ReturnResult.ok().data("publishCourse", publishCourseInfo);
    }

    // 课程最终发布
    // 修改课程状态
    @PostMapping("/publishCourse/{id}")
    public ReturnResult publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(Constants.PUBLISH_STATUS); // 设置课程发布状态
        eduCourse.setId(id);
        try {
            boolean flag = eduCourseService.updateById(eduCourse);
            if (!flag) {
                return ReturnResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnResult.ok();
    }

    // 查询所有课程列表
    @GetMapping("/queryAllCourse")
    @ApiOperation("查询所有课程列表")
    public ReturnResult queryAllCourse() {
        List<EduCourse> list = eduCourseService.list(null);
        return ReturnResult.ok().data("courseList", list);
    }

    // 分页查询
    // 多条件查询课程带分页
    @ApiOperation(value = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public ReturnResult pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                                            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                                            @RequestBody(required = false) CourseQuery courseQuery) {//通过封装courseQuery对象来直接传递查询条件
        //创建分页page对象
        Page<EduCourse> pageParam = new Page<>(page, limit);

        //调用方法实现多条件分页查询
        eduCourseService.pageQuery(pageParam, courseQuery);

        //获取查询到的数据
        List<EduCourse> records = pageParam.getRecords();

        //获取总记录数
        long total = pageParam.getTotal();
        return ReturnResult.ok().data("total", total).data("rows", records);
    }

    //课程列表中删除课程方法
    @DeleteMapping("/removeCourseById/{id}")
    @ApiOperation("课程列表中删除课程方法")
    public ReturnResult removeCourseById(@PathVariable String id) {
        eduCourseService.removeCourse(id);
        return ReturnResult.ok();
    }
}

